package com.springcloud.demo.controller;

import com.springcloud.demo.config.ConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ConfigController {

    @Autowired
    private ConfigProperties configProperties;

    @Value("${name}")
    private String name;

    @Value("${rabbitmq.host:}")
    private String host;
    @Value("${rabbitmq.port:}")
    private String port;
    @Value("${rabbitmq.username:rabbitmq}")
    private String username;
    @Value("${rabbitmq.password:rabbitmq}")
    private String password;

    @Value("${commonName:}")
    private String commonName;


    @GetMapping("/name")
    public String getName() {
        return configProperties.getName();
    }

    @GetMapping("/mysql")
    public Map<Object, Object> getMySQLProperties() {
        // JDK9中的新特性，快速创建只读集合。
//        return Map.of("host", configProperties.getMysqlHost(),
//                "port", configProperties.getMysqlPort(),
//                "username", configProperties.getMysqlUsername(),
//                "password", configProperties.getMysqlPassword());
        Map<Object, Object> map = new HashMap<>();
        map.put("host",configProperties.getMysqlHost());
        map.put("port",configProperties.getMysqlPort());
        map.put("username",configProperties.getMysqlUsername());
        map.put("password",configProperties.getMysqlPassword());
        return map;
    }

    @GetMapping("/mq")
    public Map<Object, Object> getRabbitMQProperties() {
        // JDK9中的新特性，快速创建只读集合。
//        return Map.of("host", configProperties.getMysqlHost(),
//                "port", configProperties.getMysqlPort(),
//                "username", configProperties.getMysqlUsername(),
//                "password", configProperties.getMysqlPassword());
        Map<Object, Object> map = new HashMap<>();
        map.put("host",host);
        map.put("port",port);
        map.put("username",username);
        map.put("password",password);
        return map;
    }


    @GetMapping("/common")
    public Map<Object, Object> getCommonByAnnotation() {
//        return Map.of("commonName", commonName);
        Map<Object, Object> map = new HashMap<>();
        map.put("commonName", commonName);
        return map;
    }
}
